package com.fhminyi.dao;

import com.fhminyi.model.User;
import com.fhminyi.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class UserDao {
    // 获取所有用户
    public List<User> getAllUsers() throws SQLException {
        List<User> users = new ArrayList<>();
        String sql = "SELECT * FROM 用户";
        try (Connection conn = DBUtil.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {
            while (rs.next()) {
                User user = new User();
                user.setUserId(rs.getInt("用户_id"));
                user.setUsername(rs.getString("用户名"));
                user.setPassword(rs.getString("密码"));
                user.setEmail(rs.getString("邮箱"));
                user.setPhone(rs.getString("电话"));
                user.setAddress(rs.getString("地址"));
                user.setRole(rs.getString("角色"));
                user.setCreateTime(rs.getTimestamp("创建时间"));
                user.setUpdateTime(rs.getTimestamp("更新时间"));
                users.add(user);
            }
        }
        return users;
    }

    // 根据用户ID获取用户
    public User getUserById(int userId) throws SQLException {
        String sql = "SELECT * FROM 用户 WHERE 用户_id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, userId);
            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    User user = new User();
                    user.setUserId(rs.getInt("用户_id"));
                    user.setUsername(rs.getString("用户名"));
                    user.setPassword(rs.getString("密码"));
                    user.setEmail(rs.getString("邮箱"));
                    user.setPhone(rs.getString("电话"));
                    user.setAddress(rs.getString("地址"));
                    user.setRole(rs.getString("角色"));
                    user.setCreateTime(rs.getTimestamp("创建时间"));
                    user.setUpdateTime(rs.getTimestamp("更新时间"));
                    return user;
                }
            }
        }
        return null;
    }

    // 添加用户
    public void addUser(User user) throws SQLException {
        String sql = "INSERT INTO 用户 (用户名, 密码, 邮箱, 电话, 地址, 角色) VALUES (?, ?, ?, ?, ?, ?)";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, user.getUsername());
            pstmt.setString(2, user.getPassword());
            pstmt.setString(3, user.getEmail());
            pstmt.setString(4, user.getPhone());
            pstmt.setString(5, user.getAddress());
            pstmt.setString(6, user.getRole());
            pstmt.executeUpdate();
        }
    }

    // 更新用户
    public void updateUser(User user) throws SQLException {
        String sql = "UPDATE 用户 SET 用户名 = ?, 密码 = ?, 邮箱 = ?, 电话 = ?, 地址 = ?, 角色 = ? WHERE 用户_id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, user.getUsername());
            pstmt.setString(2, user.getPassword());
            pstmt.setString(3, user.getEmail());
            pstmt.setString(4, user.getPhone());
            pstmt.setString(5, user.getAddress());
            pstmt.setString(6, user.getRole());
            pstmt.setInt(7, user.getUserId());
            pstmt.executeUpdate();
        }
    }

    // 删除用户
    public void deleteUser(int userId) throws SQLException {
        String sql = "DELETE FROM 用户 WHERE 用户_id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, userId);
            pstmt.executeUpdate();
        }
    }
}